<template>
  <div class="spinfo" v-if="spInfo">
    <div class="sspp">
      <a class="fanhui" @click="$router.back()"> </a>
      <div class="splb">
        <a :id="i" :href="v.param" v-for="(v, i) in spInfo.tabs" :key="i">
          {{ v.name }}
        </a>
      </div>
      <span class="cdl"></span>
    </div>
    <div class="lbt">
      <swiper ref="mySwiper" v-if="spInfo.datas" :options="swiperOptions">
        <swiper-slide v-for="(v, i) in spInfo.datas[0].photos" :key="i">
          <img v-if="v" width="100%" :src="v.image" alt="" />
        </swiper-slide>
        <div class="swiper-pagination" slot="pagination"></div>
      </swiper>
    </div>
    <div v-if="yhInfo" class="yhmk">
      <div v-if="yhInfo.activity_tips">
        <img :src="yhInfo.activity_tips.firstbackgroudPhoto" alt="" />
        <span>￥{{ yhInfo.activity_tips.firstDescribe.describe_price }}</span>
        <span>{{ yhInfo.activity_tips.firstDescribe.describe_subject }}</span>
        <p>
          <span>{{ yhInfo.activity_tips.secendDescribe.describe_subject }}</span
          ><del>￥{{ yhInfo.activity_tips.secendDescribe.sale_price }}</del>
        </p>
      </div>
      <div v-if="yhInfo.activity_tips">
        <img :src="yhInfo.activity_tips.secendbackgroudPhoto" alt="" />
        <span>{{ yhInfo.activity_tips.thirdDescribe.describe_subject }}</span>
        <p v-if="hDate">
          <span>{{ hDate }}</span
          >:<span>{{ mDate }}</span
          >:<span>{{ sDate }}</span>
        </p>
      </div>
    </div>
    <div class="infoName">
      <p>{{ spInfo.datas[1].subject }}</p>
      <p>{{ spInfo.datas[1].presubject }}</p>
    </div>
    <div class="huitiao">
      <div class="guig" v-if="spInfo.datas[4].formats">
        <span>{{ spInfo.datas[4].formats[0].name }}</span>
        <p>
          <span v-for="(v, i) in spInfo.datas[4].formats[0].option" :key="i">
            {{ v.name }}
          </span>
        </p>
      </div>
      <div class="gmsl">
        <p>购买数量</p>
        <div>
          <div>-</div>
          <div>1</div>
          <div>+</div>
        </div>
      </div>
      <div class="z9930">
        <div v-for="(item, index) in spInfo.datas[7].list" :key="index">
          <img :src="item.icon" alt="" />
          <span>{{ item.text }}</span>
        </div>
      </div>
      <div class="sjinfo">
        <div v-if="spInfo.datas[9].logo">
          <img :src="spInfo.datas[9].logo.image" alt="" />
        </div>
        <div v-if="spInfo.datas[9].sign">
          <p>{{ spInfo.datas[9].title }}</p>
          <p>{{ spInfo.datas[9].sign.text }}</p>
        </div>
        <div>+关注</div>
      </div>
      <div class="sjshu">
        <div v-for="(v, i) in spInfo.datas[9].list" :key="i">
          <p>{{ v.value }}</p>
          <p>{{ v.title }}</p>
        </div>
      </div>
    </div>
    <div class="addgwc">
      <div>
        <div>
          <img src="../../assets/shoucang.png" alt="" />
          <p>收藏</p>
        </div>
        <div>
          <img src="../../assets/gwc.png" alt="" />
          <p>购物车</p>
        </div>
      </div>
      <div @click="addgwc">加入购物车</div>
    </div>
    <div class="zhanwei"></div>
    <addvit class="addcg" v-show="isAdd"></addvit>
  </div>
</template>

<script>
import addvit from "./addvit.vue";
export default {
  name: "spInfo",
  data() {
    return {
      spInfo: null,
      yhInfo: null,
      swiperOptions: {
        pagination: {
          el: ".swiper-pagination",
          type: "fraction",
        },
        autoplay: true,
        loop: true,
      },
      hDate: null,
      mDate: null,
      sDate: null,
      gid: null,
      isAdd: false,
    };
  },
  methods: {
    async reqInfo(gid) {
      //    this.$route.query.gid
      //   console.log(this.$route.query.gid);
      let data = await this.axios(
        `https://mallcdn.api.epet.com/v3/goods/detail/main.html?gid=${gid}&extend_pam=buytype:|tid:0&version=410&system=wap&isWeb=1&distinct_id=17c4dcad9d349b-0277e5edad1932-5437971-250125-17c4dcad9d4913&_=1633493083725`
      );
      //   console.log(data);
      this.spInfo = data.data;
    },
    async reqth(gid) {
      let data = await this.axios(
        `https://mall.api.epet.com/v3/goods/detail/main.html?do=GetDynamic&gid=${gid}&extend_pam=buytype:|tid:0&version=410&system=wap&isWeb=1&distinct_id=17c4dcad9d349b-0277e5edad1932-5437971-250125-17c4dcad9d4913&_=1633497619138`
      );
      // console.log(data);
      this.yhInfo = data.data;
    },
    getTimeFunc(a) {
      // 当前秒数
      // 当前时间点距离1970-1-1日的总秒数；
      let currentTime = Math.round(new Date().getTime() / 1000);
      // 两者相减，得到当前时间距离结束时间的总秒数；
      let totalSeconds = a - currentTime;
      // 根据总秒数，计算时分秒
      let seconds = totalSeconds % 60;
      let totalMinutes = parseInt(totalSeconds / 60);
      let minutes = totalMinutes % 60;
      let hours = parseInt(totalMinutes / 60);
      this.hDate = hours < 10 ? "0" + hours : hours;
      this.mDate = minutes < 10 ? "0" + minutes : minutes;
      this.sDate = seconds < 10 ? "0" + seconds : seconds;
      //   console.log(hours, minutes, seconds);
    },
    addgwc() {
      let ytj = JSON.parse(localStorage.getItem("goCarList"));
      if (ytj) {
        let isAdd = ytj.some((a) => {
          return a.gid == this.gid;
        });
        if (isAdd) {
          alert("已添加！请勿重复添加");
          return;
        }
      }
      this.isAdd = true;
      let v = {
        gid: this.gid*1, 
        photo: this.spInfo.datas[0].photos[0].image || null,
        subject: this.spInfo.datas[1].subject,
        sale_price: this.yhInfo.activity_tips.firstDescribe.describe_price,
        shuNum: 1,
      };

      if (localStorage.getItem("goCarList")) {
        localStorage.setItem(
          "goCarList",
          JSON.stringify(
            JSON.parse(localStorage.getItem("goCarList")).concat([v])
          )
        );
      } else {
        localStorage.setItem("goCarList", JSON.stringify([v]));
      }
    },
  },
  created() {
    // console.log(this.$route.query.gid);
    this.busvue.$on("isaddcg", (data) => {
      this.isAdd = data;
    });
  },
  components: {
    addvit,
  },
  activated() {
    this.gid = this.$route.query.gid;
    setInterval(() => {
      this.getTimeFunc(this.yhInfo.activity_tips.fourthDescribe.timestamp);
    }, 1000);
    this.reqInfo(this.gid);
    this.reqth(this.gid);
  },
};
</script>

<style scoped lang="less">
.addcg {
  position: fixed;
  z-index: 200;
  top: 0;
  left: 0;
  width: 100vw;
  height: 13.34rem !important;
  background-color: rgba(0, 0, 0, 0.521);
}
.addgwc {
  width: 7.5rem !important;
  height: 1rem !important;
  background-color: #fff;
  font-size: 0.3rem;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 100;
  display: flex;
  justify-content: space-between;
  > div:nth-child(1) {
    width: 50%;
    display: flex;

    > div {
      width: 50%;
      text-align: center;
      padding-top: 0.2rem;
      > img {
        width: 0.4rem;
      }
      > p {
        margin: 0;
        font-size: 0.24rem;
        color: rgb(153, 153, 153);
      }
    }
  }
  > div:nth-child(2) {
    width: 50%;
    background-color: red;
    color: rgb(255, 255, 255);
    height: 1rem;
    line-height: 1rem;
    text-align: center;
  }
}
.zhanwei {
  position: relative;
  z-index: 80;
  height: 1rem;
  width: 100%;
}
.yhmk {
  width: 100%;
  font-size: 0.26rem;
  display: flex;
  > div:nth-child(1) {
    width: 4.86rem;
    height: 1.3rem;
    position: relative;
    > img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 1.3rem;
    }
    > span:nth-child(2) {
      color: rgb(230, 182, 134);
      font-size: 0.32rem;
      font-weight: 700;
    }
    > span,
    p {
      color: #fff;
      position: relative;
      z-index: 100;
    }
  }
  > div:nth-child(2) {
    position: relative;
    width: 2.62rem;
    height: 1.3rem;
    text-align: center;
    padding: 0.15rem;
    box-sizing: border-box;
    > img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 1.3rem;
    }
    > span,
    p {
      position: relative;
      z-index: 100;
    }
    > p {
      font-size: 0.26rem;
      font-weight: 700;
      color: rgb(173, 115, 64);
      margin: 0.1rem 0;
      > span {
        display: inline-block;
        width: 0.4rem;
        height: 0.4rem;
        border-radius: 0.1rem;
        text-align: center;
        line-height: 0.4rem;
        background-color: #fff;
      }
    }
  }
}
.infoName {
  font-size: 0.26rem;
  padding-left: 0.2rem;
  box-sizing: border-box;
  width: 100%;
  height: 1.02rem;
  > p:nth-child(2) {
    font-size: 0.24rem;
    color: rgb(255, 117, 117);
    margin: 0;
  }
}
.huitiao {
  width: 100%;
  padding-top: 0.2rem;
  background-color: rgb(243, 244, 245);
  .sjshu {
    font-size: 0.24rem;
    background-color: #fff;
    display: flex;
    justify-content: space-around;
    > div {
      width: 30%;
      text-align: center;
      line-height: 0.1rem;
      > p:nth-child(2) {
        color: rgb(138, 138, 138);
      }
    }
    > div:nth-child(2) {
      border-right: 1px solid rgb(202, 202, 202);
      border-left: 1px solid rgb(202, 202, 202);
    }
  }
  .guig {
    font-size: 0.24rem;
    background-color: #fff;
    width: 7.1rem;
    height: 0.62rem;
    padding: 0.2rem;
    display: flex;
    margin-bottom: 0.2rem;
    > p {
      > span {
        padding: 0.09rem 0.4rem;
        border: 0.02rem solid rgb(167, 167, 167);
        border-radius: 0.1rem;
        margin-left: 0.2rem;
        //   color: rgb(253, 253, 253);
        //   background-color: rgb(255, 52, 52);
      }
    }
  }
  .gmsl {
    width: 7.1rem;
    height: 0.64rem;
    padding: 0.2rem;
    font-size: 0.3rem;
    display: flex;
    background-color: #fff;
    margin-bottom: 0.2rem;
    align-items: center;
    > div {
      display: flex;
      margin-left: 0.2rem;
      width: 2.16rem;
      height: 0.64rem;
      background-color: rgb(255, 255, 255);
      line-height: 0.64rem;
      text-align: center;
      border-radius: 0.1rem;
      overflow: hidden;
      > div:nth-child(1) {
        width: 0.6rem;
        border: 0.02rem solid rgb(207, 207, 207);
        border-radius: 0.1rem 0 0 0.1rem;
      }
      > div:nth-child(2) {
        border: 0.01rem solid rgb(207, 207, 207);
        width: 0.96rem;
        color: red;
      }
      > div:nth-child(3) {
        width: 0.6rem;
        border: 0.02rem solid rgb(207, 207, 207);
        border-radius: 0 0.1rem 0.1rem 0;
      }
    }
  }
  .z9930 {
    width: 100%;
    display: flex;
    background-color: #fff;
    margin-bottom: 0.2rem;
    padding: 0.2rem 0;
    justify-content: space-around;
    > div {
      font-size: 0.3rem;
      width: 30%;
      display: flex;
      align-items: center;
      text-align: center;
      padding-left: 0.2rem;
      box-sizing: border-box;
      background-color: rgb(255, 255, 255);
      > img {
        width: 0.4rem;
        margin-right: 0.1rem;
      }
    }
  }
  .sjinfo {
    display: flex;
    box-sizing: border-box;
    width: 100%;
    padding: 0.2rem;
    background-color: #fff;
    font-size: 0.26rem;
    > div > img {
      width: 1.8rem;
      height: 0.9rem;
    }
    > div:nth-child(2) {
      > p {
        margin: 0;
      }
    }
    > div:nth-child(3) {
      margin-left: 1rem;
      font-size: 0.26rem;
      width: 1.06rem;
      height: 0.52rem;
      line-height: 0.52rem;
      padding: 0.04rem 0.12rem;
      text-align: center;
      border-radius: 0.1rem;
      color: red;
      border: 0.02rem solid red;
    }
  }
}
.fanhui {
  margin: 0;
  position: absolute;
  left: 5px;
  width: 35px;
  height: 35px;
  top: 7px;
  background: url(https://static.epetbar.com/static_web/wap/dist/images/background/topIco.png)
    0 0 no-repeat;
  background-size: 35px auto;
  background-position: 0 0;
}
.swiper-pagination {
  font-size: 0.3rem;
  width: 0.6rem;
  height: 0.6rem;
  line-height: 0.6rem;
  border-radius: 0.3rem;
  color: rgb(255, 255, 255);
  background-color: rgb(180, 180, 180);
  position: absolute;
  bottom: 0.2rem;
  left: 90%;
}
.sspp {
  width: 100%;
  border-bottom: 1px solid rgb(240, 240, 240);
}
.splb {
  font-size: 0.36rem;
  line-height: 1rem;
  width: 100%;
  text-align: center;
  > a {
    color: #000;
  }
}

.cdl {
  margin: 0;
  top: 7px;
  right: 5px;
  width: 35px;
  height: 35px;
  background: url(https://static.epetbar.com/static_web/wap/dist/images/background/topIco.png)
    0 0 no-repeat;
  background-size: 35px auto;
  position: absolute;
  background-position: 0 -70px;
}
</style>